package com.codoon.unova.logic;

import java.util.Stack;

import com.codoon.unova.util.LogUtil;

import android.app.Activity;

public class ActivityManager {

	private static Stack<Activity> activityStack;
	private static ActivityManager instance;
	private boolean flag = true;

	private ActivityManager() {
	}

	public static ActivityManager getInstance() {
		if (instance == null) {
			instance = new ActivityManager();
		}
		return instance;
	}

	// 退出栈顶Activity
	public void popActivity(Activity activity) {
//		LogUtil.info("--------###---" + activityStack.size());
		if (activity != null) {
//			LogUtil.info("--------***---" + activityStack.size());
			activity.finish();
			activityStack.remove(activity);
			if (activityStack.size() == 0) {
				flag = false;
			}
			activity = null;
		}
	}

	// 获得当前栈顶Activity
	public Activity currentActivity() {
		Activity activity = activityStack.lastElement();
		return activity;
	}

	public Stack<Activity> getActivityStack() {
		return activityStack;
	}

	// 将当前Activity推入栈中
	public void pushActivity(Activity activity) {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		activityStack.add(activity);
	}

	// 退出栈中所有Activity
	public void popAllActivityExceptOne(Class cls) {
		flag = true;
		while (flag) {
			Activity activity = currentActivity();
			if (activity == null) {
				break;
			}
			if (activity.getClass().equals(cls)) {
				break;
			}
			popActivity(activity);
		}
	}

	// 退出栈中所有Activity
	public void popAllActivityExceptOne() {
		flag = true;
//		LogUtil.info("---activityStack.size=" + activityStack.size());
		while (flag) {
			Activity activity = currentActivity();
			if (activity == null) {
				break;
			}
			popActivity(activity);
		}
	}

}
